//------------------------------------------------------------------------------
// The contents of this file are subject to the nopCommerce Public License Version 1.0 ("License"); you may not use this file except in compliance with the License.
// You may obtain a copy of the License at  http://www.nopCommerce.com/License.aspx. 
// 
// Software distributed under the License is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. 
// See the License for the specific language governing rights and limitations under the License.
// 
// The Original Code is nopCommerce.
// The Initial Developer of the Original Code is NopSolutions.
// All Rights Reserved.
// 
// Contributor(s): _______. 
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Text;
using Tathata.Common.Caching;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Data.Common;
using System.Data;
using System.Globalization;
using Tathata.Common.DAL;
using Tathata.Common.BLL.Entities;
using Tathata.Common.BLL.EntityCollections;
using Tathata.Common.DAL.Managers;

namespace Tathata.Common.BLL.Managers
{
    /// <summary>
    /// Measure dimension manager
    /// </summary>
    public class MeasureDimensionManager
    {
        #region Constants
        private const string MEASUREDIMENSIONS_ALL_KEY = "Nop.measuredimension.all";
        private const string MEASUREDIMENSIONS_BY_ID_KEY = "Nop.measuredimension.id-{0}";
        #endregion

        #region Methods
        /// <summary>
        /// Gets a measure dimension by identifier
        /// </summary>
        /// <param name="MeasureDimensionID">Measure dimension identifier</param>
        /// <returns>Measure dimension</returns>
        public static MeasureDimension GetByMeasureDimensionID(int MeasureDimensionID)
        {
            string key = string.Format(MEASUREDIMENSIONS_BY_ID_KEY, MeasureDimensionID);
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (MeasureDimension)obj2;
            }

            MeasureDimension measureDimension = MeasureDimensionDBManager.GetByMeasureDimensionID(MeasureDimensionID);

            if (NopContext.IsCachable)
            {
                NopCache.Max(key, measureDimension);
            }
            return measureDimension;
        }


        /// <summary>
        /// Gets all measure dimensions
        /// </summary>
        /// <returns>Measure dimension collection</returns>
        public static MeasureDimensionCollection GetAll()
        {
            string key = MEASUREDIMENSIONS_ALL_KEY;
            object obj2 = NopCache.Get(key);
            if (NopContext.IsCachable && (obj2 != null))
            {
                return (MeasureDimensionCollection)obj2;
            }

            MeasureDimensionCollection measureDimensionCollection = MeasureDimensionDBManager.GetAll();
            if (NopContext.IsCachable)
            {
                NopCache.Max(key, measureDimensionCollection);
            }
            return measureDimensionCollection;
        }

        #endregion

        #region Properties
        /// <summary>
        /// Gets or sets the dimension that will be used as a default
        /// </summary>
        public static MeasureDimension BaseDimensionIn
        {
            get
            {
                int baseDimensionIn = SettingManager.GetSettingValueInteger("Common.BaseDimensionIn");
                return MeasureDimensionManager.GetByMeasureDimensionID(baseDimensionIn);
            }
            set
            {
                if (value != null)
                    SettingManager.SetParam("Common.BaseDimensionIn", value.MeasureDimensionID.ToString(), string.Empty);
            }
        }
        #endregion
    }
}
